//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using System.Text;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for TagEditor.
	/// </summary>
	public class TagEditor : TemplatedWebControl
	{
		protected TextBox Tags;
		protected IModalButton SelectTags;
		protected HtmlInputHidden AllTags;

		#region Properties

		ArrayList _availablePostCategories = new ArrayList();
		public ArrayList AvailableTags
		{
			get
			{
				return _availablePostCategories;
			}
			set
			{
				_availablePostCategories = value;
			}
		}

		public string[] _selectedTags = new string[0];
		public string[] SelectedTags
		{
			get
			{
				return _selectedTags;
			}
			set
			{
				_selectedTags = value;
			}
		}

		#endregion

		#region Skin

		protected override void AttachChildControls() 
		{
			Tags = (TextBox) FindControl("Tags");
			SelectTags = (IModalButton) FindControl("SelectTags");

			AllTags = new HtmlInputHidden();
			AllTags.Name = "AllTags";
			AllTags.ID = "AllTags";

			this.Controls.Add(AllTags);
		}

		#endregion

		#region Control Event Handlers

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			EnsureChildControls();

			if (Page.IsPostBack)
			{
				String[] newTags = Tags.Text.Split(new char[] {';', ','});
				ArrayList tags = new ArrayList();
				for (int i = 0; i < newTags.Length; i++)
				{
					newTags[i] = PostCategories.FormatName(newTags[i].Trim());
					if (!Globals.IsNullorEmpty(newTags[i]) && !tags.Contains(newTags[i]))
						tags.Add(newTags[i]);
				}

				this.SelectedTags = (string[]) tags.ToArray(typeof(string));
			}

			DataBind();
		}

		public override void DataBind()
		{
			base.DataBind ();

			StringBuilder list = new StringBuilder();
			foreach (PostCategory tag in this.AvailableTags)
			{
				if (list.Length > 0)
					list.Append("&");

				list.Append(Globals.UrlEncode(System.Web.HttpUtility.HtmlDecode(tag.Name)));
				list.Append("&");
				list.Append(Globals.UrlEncode(tag.Name));
			}
			AllTags.Value = list.ToString();

            list = new StringBuilder();
			foreach (string tag in this.SelectedTags)
			{
				if (list.Length > 0)
					list.Append("; ");

				list.Append(System.Web.HttpUtility.HtmlDecode(tag));
			}
			Tags.Text = list.ToString();

			if (SelectTags != null)
			{
				SelectTags.Url = SiteUrls.Instance().TagSelectorModal(Globals.UrlEncode(this.ClientID));
				
				Control c = SelectTags as Control;
				if (c != null)
					c.Visible = (this.AvailableTags != null && this.AvailableTags.Count > 0);
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (!this.Page.IsClientScriptBlockRegistered(this.GetType().FullName))
				this.Page.RegisterClientScriptBlock(this.GetType().FullName, string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/TagEditor.js")));

			if (!this.Page.IsStartupScriptRegistered(this.ClientID))
				this.Page.RegisterStartupScript(this.ClientID, string.Format("<script language=\"javascript\">var {0} = new TagEditor('{0}', '{1}', '{2}');</script>", this.ClientID, this.AllTags.ClientID, this.Tags.ClientID));
		}

		#endregion
	}
}
